package com.zhiyou.hospital.ssh.action;

import com.opensymphony.xwork2.ActionContext;
import com.zhiyou.hospital.ssh.dao.IPermissionDao;
import com.zhiyou.hospital.ssh.model.PermissionModel;
import com.zhiyou.hospital.ssh.model.RoleModel;
import com.zhiyou.hospital.ssh.service.IPermissionService;
import com.zhiyou.hospital.ssh.service.IRoleService;
import com.zhiyou.hospital.ssh.util.LogHelper;
import com.zhiyou.hospital.ssh.util.LogUtil;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by zhouyanchao on 2017/10/19.
 * Descr:
 */
@Namespace("/role")
@ParentPackage("defaultPackage")
public class RoleAction extends BaseAction {
	LogUtil log = LogHelper.getLogUtilInstance(RoleAction.class);

	RoleModel role;

	public RoleModel getRole() {
		return role;
	}

	public void setRole(RoleModel role) {
		this.role = role;
	}

	@Autowired
	IRoleService roleService;

	@Autowired
	IPermissionService permissionService;


	@Action(value = "add",results = {@Result(name = "success",location = "/WEB-INF/jsp/role_add.jsp"),
			@Result(name = "result",location = "/WEB-INF/jsp/test.jsp")})
	public String addTest(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String method=request.getMethod();

		if(method.equalsIgnoreCase(REQ_GET)){
			log.i("准备添加角色");

			//获取所有角色
			List<PermissionModel> permissionModels = permissionService.queryAllPermissions();
			ActionContext.getContext().put("permissions",permissionModels);

			return SUCCESS;
		}

		if(method.equalsIgnoreCase(REQ_POST)){
			//处理添加用户信息
			System.out.println(role);
			System.out.println(role.getPermissions());


			roleService.addNewData(role);


			return "result";
		}

		return SUCCESS;
	}

	@Action(value = "delete",results = {@Result(name = "success",location = "/WEB-INF/jsp/test.jsp")})
	public String delete(){
		HttpServletRequest request = ServletActionContext.getRequest();
		int id = Integer.parseInt(request.getParameter("id")==null?"0":request.getParameter("id"));

		if(id>0){
			RoleModel role = roleService.getById(RoleModel.class,id);
			roleService.deleteData(role);
		}

		return SUCCESS;
	}


}
